package com.liry.security.controller;

import com.liry.security.config.CacheManager;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ALI
 * @since 2023/5/31
 */
@RestController
public class LoginController {

    @GetMapping("/login.html")
    public String loginPage() {
        return "login";
    }

    @GetMapping("/index.html")
    public String index() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(authentication);
        return "index";
    }

    @GetMapping("/main.html")
    public String main() {
        return "main";
    }

    @GetMapping("/test/1")
    public String test() {
        return "test";
    }

    @Autowired
    private CacheManager cacheManager;
    @GetMapping("/set")
    public String set(Long time) {
        cacheManager.set("abc", "abc", time, TimeUnit.SECONDS);
        return "ok";
    }

    @GetMapping("/get")
    public Long get() {
        Object abc = cacheManager.get("abc");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return cacheManager.getExpire("abc");
    }
}
